Page({
  /**
   * 页面的初始数据
   */
  data: {
    name:'',
    sex:'',
    grade:'',
    cs:'',
    phone:'',
    QQ:'',
    infList:{
      name:'',
      sex:'',
      grade:'',
      cs:'',
      phone:'',
      QQ:'',
    },
  },
  inputname(e){
    this.setData({
      name:e.detail.value,
    })
  },
  inputsex(e){
    this.setData({
      sex:e.detail.value,
    })
  },
  inputgrade(e){
    this.setData({
      grade:e.detail.value,
    })
  },
  inputcs(e){
    this.setData({
      cs:e.detail.value,
    })
  },
  inputphone(e){
    this.setData({
      phone:e.detail.value,
    })
  },
  QQ(e){
    this.setData({
      QQ:e.detail.value,
    })
  },
  save(e){
    if((!this.data.phone)||(!this.data.name)||(!this.data.sex)||(!this.data.grade)||(!this.data.cs)||(!this.data.QQ))
    {
      wx.showToast({
        title: '请输入完整',
        icon:'error',
      })
    }
    else{
      var that = this;
      this.setData({
        infList:{
          name:that.data.name,
          sex:that.data.sex,
          grade:that.data.grade,
          cs:that.data.cs,
          phone:that.data.phone,
          QQ:that.data.QQ,
        }
      }) 
      wx.showLoading({
        title: '加载中',
      })
      var app = getApp();
      const openid = app.globalData.useropenid;
      const DB = wx.cloud.database().collection("infList");
      DB.where({_openid:openid}).get({
        success(e){
          const _id = e.data[0]._id;
          const DB2 = wx.cloud.database().collection("infList");
          DB2.doc(_id).set({
              data:that.data.infList,
              success(e){ 
                //console.log(e);
                wx.hideLoading({
                  success: (res) => {
                    wx.showToast({
                      title: '修改成功',
                    })
                    wx.switchTab({
                      url: '../my/my',
                    })
                  },
                })
              }
            })
        }
      })
    }
  },
  onLoad()
  {
    var app = getApp();
    if(!app.globalData.hasLogin)
    {
      setTimeout(
        function () { //注意function这里不能缺少
          wx.switchTab({
            url: '../my/my',
          })
        }, 1500)
      wx.showToast({
        title: '请登录后再查看',
        icon:"error",
      })
      return;
      }
    var that = this;
    var app  = getApp();
    const openid = app.globalData.useropenid;
    const DB3 = wx.cloud.database().collection("infList");
    DB3.where({_openid:openid}).get({
      success: function(res) {  
        const templist = res.data;  
        //console.log(templist);
        if(!templist.length)
        { 
          var addtemplist={
            name:'',
            sex:'',
            grade:'',
            cs:'',
            phone:'',
            QQ:'',
          } 
          const DB4 = wx.cloud.database().collection("infList");
          DB4.add({
            data:addtemplist, 
          })
        }   
        else{  
          that.setData(
            {
              infList:templist,
            }
          ); 
        }
      }
    })
  },
})
